package com.wlz.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

/*    @Value("${spring.servlet.multipart.max-file-size}")
    private String fileSize;*/

    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */



    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = IOException.class)
    @ResponseBody
    public ResultBody bizExceptionHandler(HttpServletRequest req, IOException e) {
        logger.error("发生业务异常！原因是：{}", e);
        return ResultBody.error("500","网络异常，文件传输失败");
    }



/*
    */
/**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     *//*

    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public ResultBody bizExceptionHandler(HttpServletRequest req, MaxUploadSizeExceededException e) {
        logger.error("发生业务异常！原因是：{}", e);
        return ResultBody.error("500","上传文件不能超过"+fileSize);
    }
*/

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
        return ResultBody.error("程序内部错误");
    }
//
//    /**
//     * 处理其他异常
//     *
//     * @param req
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(value = BadCredentialsException.class)
//    @ResponseBody
//    public ResultBody exceptionHandler(HttpServletRequest req, BadCredentialsException e) {
//        logger.error("未知异常！原因是:", e);
//        String message = e.getMessage();
//        if(e.getMessage()!=null){
//            return ResultBody.error("500",e.getMessage());
//        }
//        return ResultBody.error(getErrorCodeAndMsg("500"));
//    }

//    public static void main(String[] args) {
//        try {
//            throw new BadCredentialsException("213");
//        }catch (BadCredentialsException e){
//            System.out.println(e.getMessage()+"___________________________________");
//            System.out.println(e+"___________________________________");
//        }
//    }

    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBody exceptionHandler(HttpServletRequest req, Exception e) {
        logger.error("未知异常！原因是:", e);
        return ResultBody.error("程序内部错误");
//        return ResultBody.error(getErrorCodeAndMsg("500"));
    }

//    /**
//     * @param errorCodes 错误码
//     * @return 错误码和错误msg
//     */
//    public static ErrorCode getErrorCodeAndMsg(String errorCodes) {
//        List<ErrorCode> errorCodeList = CommonResultInfo.getInstance().getErrorCode();
//        List<ErrorCode> errorCode = errorCodeList.stream()
//                .filter(k -> k.getResultCode().equals(errorCodes)).collect(Collectors.toList());
//        if (null == errorCode || errorCode.size() == 0) {
//            logger.error("未知异常！原因是:" + "数据库没有保存当前异常状态码");
//            throw new NullPointerException();
//        }
//        return errorCode.get(0);
//    }
}
